//
//  XLGCurrentUser.m
//  etionCBLogic
//
//  Created by wu jingxing on 13-6-7.
//  Copyright (c) 2013年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGCurrentUser.h"

#define CurrentUserHeadImagePath        @"CurrentUserHeadImage"

NSString *const CurrentUserCurrentUserHeadImageUpdateNotification = @"CurrentUserCurrentUserHeadImageUpdateNotification";

@interface CCurrentUser ()<XMSSimpleDownloadManagerDelegate>
{
    XMSSimpleDownloadManager *_sdm;
    
    __unsafe_unretained id<CCurrentUserBaseAttributeDelegate> _user;
}

@property(nonatomic,readonly) XMSSimpleDownloadManager *sdm;
@property(nonatomic,retain) id<CCurrentUserBaseAttributeDelegate> user;

@end

@implementation CCurrentUser

static CCurrentUser *s_user;

-(id)init
{
    self = [super init];
    
    _sdm = [XMSSimpleDownloadManager new];
    [_sdm initDownloadQueue:self];
    
    return self;
}

-(void)dealloc
{
    [_sdm release_mm];
    [_user release_mm];
    superdealloc_mm;
}

-(void)simpleDownloadManager:(XMSSimpleDownloadManager *)sdm finishedDownload:(CSimpleDownloadTaskTag *)tag
{
    s_user.user.headImagePath = tag.filePath;
    
    DispatchMainThreadAsync(^
    {
        if ([XMSFileHelper isFileExist:tag.filePath] == YES)
            [NSNotificationCenter postNotification:CurrentUserCurrentUserHeadImageUpdateNotification param:tag.filePath];
    });
}

+ (void)initCurrentUser:(id<CCurrentUserBaseAttributeDelegate>)p
{
    if(s_user == nil)
        s_user = [CCurrentUser new];
    
        s_user.user = p;
        
        if (s_user.user.headImageURL > 0 && [s_user.user.headImageURL isHttpString])
        {
            NSString *dir = [[XLGContext context].m_szUserpath stringByAppendingPathComponent:CurrentUserHeadImagePath];
            
            if([XMSFileHelper isDirExist:dir] != YES)
                [XMSFileHelper createDir:dir];
            
            NSString *szPath = [dir stringByAppendingPathComponent:[s_user.user.headImageURL lastPathComponent]];
            if ([XMSFileHelper isFileExist:szPath] != YES && [s_user.sdm isContainDownloadURL:s_user.user.headImageURL] == NO)
            {
                [s_user.sdm cancelAllDownloadTask];
                [XMSFileHelper deleteFileDir:dir];
                [XMSFileHelper createDir:dir];
                [s_user.sdm addDownloadTask:s_user.user.headImageURL path:szPath attachment:nil];
            }
            else
            {
                s_user.user.headImagePath = szPath;
                [NSNotificationCenter postNotification:CurrentUserCurrentUserHeadImageUpdateNotification param:szPath];
            }
        }
}

+ (void)releaseCurrentUser
{
    [s_user.sdm cancelAllDownloadTask];
    [s_user.sdm releaseDownloadQueue];
    ReleaseObj(s_user);
}

+ (UIImage*)currentUserHeadImage
{
    if(s_user.user.headImagePath.length <=0)
        return s_user.user.defaultHeadImage;
    else
    {
        NSString *dir = [[[XLGContext context].m_szUserpath stringByAppendingPathComponent:CurrentUserHeadImagePath] stringByAppendingPathComponent:[s_user.user.headImagePath lastPathComponent]];
        if([XMSFileHelper isFileExist:dir] == YES)
            return [UIImage imageWithContentsOfFile:dir];
    }
    return s_user.user.defaultHeadImage;
}

+ (void)copyAndCacheNewHeadImagePath:(NSString*)filePath
{
    [CCurrentUser copyAndCacheNewHeadImagePath:filePath withNewFileName:[filePath lastPathComponent]];
}

+ (void)copyAndCacheNewHeadImagePath:(NSString*)filePath withNewFileName:(NSString*)newFileName
{
    NSString *dir = [[XLGContext context].m_szUserpath stringByAppendingPathComponent:CurrentUserHeadImagePath];
    
    [XMSFileHelper deleteFileDir:dir];
    [XMSFileHelper createDir:dir];
    
    dir = [dir stringByAppendingPathComponent:newFileName];
    
    [XMSFileHelper copySource:filePath toTarget:dir];
    
    s_user.user.headImagePath = dir;
    
    [NSNotificationCenter postNotification:CurrentUserCurrentUserHeadImageUpdateNotification param:dir];
}

+ (id<CCurrentUserBaseAttributeDelegate>)currentUser
{
    return s_user.user;
}

@end
